package nl.haemers.managers 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class SoundObject extends EventDispatcher
	{
		private var _soundChannel			:SoundChannel;
		private var _soundObject			:Sound;
		private var _soundTransform			:SoundTransform;
		
		private var _key					:String;
		private var _volume					:Number;
		private var _position				:Number = 0;
		private var _loops					:int = 0;
		private var _infinityLoop			:Boolean;
		
		private var _mute					:Boolean;
		private var _playing				:Boolean;
		
		public function SoundObject(sound:Sound = null, soundTransform:SoundTransform = null) 
		{
			_soundObject = sound;
			_soundTransform = soundTransform;
			if (_soundTransform != null)
				_volume = _soundTransform.volume;
		}
		
		/** Public methods **/
		public function play() : void
		{
			_playing = true;
			_soundChannel = _soundObject.play(_position, _loops, _soundTransform);
			
			if (!_soundChannel.hasEventListener(Event.SOUND_COMPLETE)) configListeners();
		}
		public function pause() : void
		{
			_playing = false;
			_soundChannel.stop();
		}
		public function stop() : void
		{
			_playing = false;
			_soundChannel.stop();
			removeEventListeners();
			_soundChannel = null;
			_position = 0;
		}
		public function reset() : void
		{
			var tPlaying:Boolean = _playing;
			stop();
			if (tPlaying) play();
				
		}
		/** Getters & Setters **/
		public function get key() : String
		{ 
			return _key; 
		}
		public function set key( value:String ) : void 
		{
			_key = value;	
		}
		public function get mute() : Boolean
		{ 
			return _mute; 
		}
		public function set mute( value:Boolean ) : void
		{
			_mute = value;
			if (_mute)
			{
				_soundTransform.volume = 0;
				_soundChannel.soundTransform = _soundTransform;
			}
			else 
			{
				_soundTransform.volume = _volume;
				_soundChannel.soundTransform = _soundTransform;
			}
		}
		public function get sound() : Sound
		{ 
			return _soundObject; 
		}
		public function set sound( value:Sound ) : void 
		{
			_soundObject = value;	
		}
		
		public function get soundChannel() : SoundChannel
		{ 
			return _soundChannel; 
		}
		
		public function get soundTransform() : SoundTransform
		{ 
			return _soundTransform; 
		}
		public function set soundTransform( value:SoundTransform ) : void 
		{
			_soundTransform = value;
			_volume = _soundTransform.volume;
		}	
		
		public function get pan() : Number
		{ 
			return _soundTransform.pan; 
		}
		public function set pan( value:Number ) : void 
		{
			_soundTransform.pan = value;	
		}
		
		public function get position() : Number
		{ 
			return _position; 
		}
		public function set position( value:Number ) : void 
		{
			_position = value;
			if (_playing)
			{
				pause();
				play();
			}
		}
		
		public function get volume() : Number
		{ 
			return _soundTransform.volume; 
		}
		public function set volume( value:Number ) : void 
		{
			_soundTransform.volume = value;
			_volume = value;
		}
		
		public function get infinityLoop() : Boolean
		{ 
			return _infinityLoop; 
		}
		public function set infinityLoop( value:Boolean ) : void 
		{
			_infinityLoop = value;	
		}
		
		public function get playing() : Boolean
		{ 
			return _playing; 
		}
		/** Protected methods **/
		
		/** Private methods **/
		private function configListeners() : void
		{
			_soundChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
		}
		private function removeEventListeners() : void
		{
			_soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
		}
		private function soundCompleteHandler( e:Event ) : void
		{
			trace("sound Completed");
			if (_infinityLoop)
			{
				reset();
				play();
			}
		}
	}

}